package hn.cch.redisson;

import hn.cch.redisson.service.ScheduleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Component
public class ScheduleApplication implements ApplicationRunner {

    private final static Logger logger = LoggerFactory.getLogger(ScheduleApplication.class);

    @Autowired
    public ScheduleService scheduleService;

    @Override
    public void run(ApplicationArguments args) {
        int corePoolSize = 4;
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
            corePoolSize, corePoolSize, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<>(corePoolSize));
        for (int i = 0; i < corePoolSize; i++) {
            threadPoolExecutor.execute(() -> {
                scheduleService.scheduleTask();
            });
        }
    }
}
